package com.java.frontweb.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.java.base.pojo.EasyUIResult;
import com.java.base.service.HttpAPIService;
import com.java.manage.pojo.TbContent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IndexService
 * @Description 前端服务
 * @Author Trigger Wu
 * @Date 2018/9/1410:51
 * @Version 1.0
 **/
@Service
public class IndexService {
    @Autowired
    private HttpAPIService apiService;

    @Value("${CQZG_MANAGE_URL}")
    private String CQZG_MANAGE_URL;

    @Value("${INDEX_AD1_URL}")
    private String INDEX_AD1_URL;

    @Value("${INDEX_AD2_URL}")
    private String INDEX_AD2_URL;

    private static final ObjectMapper MAPPER = new ObjectMapper();
    /*
     *  大广告设置
     * 
     * @author Tigger Wu
     * @date 2018/9/14 22:37
     * @param []
     * @return java.lang.String
     */
    @SuppressWarnings("unchecked")
    public String queryIndexAD1() {
        try {
            String url = CQZG_MANAGE_URL + INDEX_AD1_URL;
            String jsonData = this.apiService.doGet(url);
            if (StringUtils.isEmpty(jsonData)) {
                return null;
            }
            // 解析json数据，封装成前端所需要的结构
            EasyUIResult easyUIResult = EasyUIResult.formatToList(jsonData, TbContent.class);
            List<TbContent> contents = (List<TbContent>) easyUIResult.getRows();
            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
            for (TbContent content : contents) {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put("srcB", content.getPic());
                map.put("height", 240);
                map.put("alt",content.getTitle());
                map.put("width", 670);
                map.put("src", content.getPic());
                map.put("widthB", 550);
                map.put("href", content.getUrl());
                map.put("heightB", 240);
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 小广告设置
     *
     * @author Tigger Wu
     * @date 2018/9/14 22:37
     * @param []
     * @return java.lang.String
     */
    public String queryIndexAD2() {
        try {
            String url = CQZG_MANAGE_URL + INDEX_AD2_URL;
            String jsonData = this.apiService.doGet(url);
            if (StringUtils.isEmpty(jsonData)) {
                return null;
            }
            // 解析json数据，封装成前端所需要的结构
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            ArrayNode rows = (ArrayNode) jsonNode.get("rows");
            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
            for (JsonNode row : rows) {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put("width", 310);
                map.put("height", 70);
                map.put("src", row.get("pic").asText());
                map.put("href", row.get("url").asText());
                map.put("alt", row.get("title").asText());
                map.put("widthB", 210);
                map.put("heightB", 70);
                map.put("srcB", row.get("pic").asText());
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
